<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {

	public function index() 
	{
		$this->load->helper('url');
		// Template
		$this->load->view('view_user');
	}
	

	// Contents
	function home() {
		$this->load->view('user_templates/page_home');
	}
	function profile() {
		session_start();
		// if($this->is_login()){
			$this->load->view('user_templates/page_profile');
		// }
		// else {
			// $this->load->view('user_templates/page_login');
		// }
		// $this->session->unset_userdata('is_logged_in');
		
	}
	function editProfile(){
		$this->load->view('user_templates/page_edit_profile');
	}
	function login(){
		$this->load->view('user_templates/page_login');
	}
	function register(){
		$this->load->view('user_templates/page_register');
	}
	//===============================================================================================//
	//========================================FUNCTION===============================================//
	//===============================================================================================//

	// PROFILE
	public function getProfile(){

		$username = $this->session->userdata('username');
		$password = $this->session->userdata('password');
		$this->load->model('Model_user');
		$cursor = $this->Model_user->getProfile($username, '');

		$arr = array();
		foreach(iterator_to_array($cursor) as $point) {
			array_push($arr, $point);
		}
		if(count($arr) > 0) {
			echo json_encode($arr[0]);
		}
		else echo 'false';
		
		
	}
	// UPDATE PROFILE
	public function getUpdateProfile() {

		$pf = $this->input->post('profile');
		$this->load->model('Model_user');
        $id = $pf['_id'];
        $q = $this->session->userdata('username');
		$username = $pf['username'];
		$password = $pf['password'];
		$fullname = $pf['fullname'];
		$dob = $pf['dob'];
		$gender = $pf['gender'];
		$address = $pf['address'];
		$email = $pf['email'];
		$phone = $pf['phone'];
		$rs = $this->Model_user->update_info($id, $q,$password, $fullname, $dob, $gender, $address, $email, $phone);  
		
	}

	// GET USER NAME
	public function getUser() {
		if( $this->session->userdata('username') != null) {
			echo 'Đã Đăng nhập';
			// redirect(base_url().'index.php/user#/home');
		}
	}

	//INSERT NEW USER
	public function insertUser() {
		$user = $this->input->post('user');
		$this->load->model('Model_user');	

		if($user['password'] != $user['repassword']) {
			echo 'nomatch';
			return;
		}
		$bool = $this->Model_user->checkExists($user['userName']);
		if($bool != null) {
			echo 'exists';
			return;
		}
		$ins = $this->Model_user->insertUser($user);
		echo $ins;
	}

	//GET LOGIN
	public function getLogin(){
		$user = $this->input->post('user');
		$username = $user['username'];
		$password = $user['password'];

		$this->load->model('Model_user');	
		$ck = null;
		$ck = $this->Model_user->getUser($username, $password);
		$arr = array();
		foreach(iterator_to_array($ck) as $point) {
			array_push($arr, $point);
		}

		if(isset($arr[0])) //if login success
        {
            $catchSession = array(
                'username' => $username,
                'is_logged_in' => true
            );
            session_start();
            $this->session->set_userdata($catchSession);
            echo 'Đăng nhập thành công';
        }  
        else 
        {
            echo 'Username hoặc Password không đúng!' ;
        }

	}

	//GET LOGOUT
	public function getLogout(){
        if($this->is_login()){
           $this->session->unset_userdata(array('username'=>'', 'is_logged_in'=>false));
    	}
	}


	//CHECK SESSION
	public function check_session() {
		if( $this->session->userdata('username') != null) {
			echo $this->session->userdata('username');
			// redirect(base_url().'index.php/user#/home');
		}
		else echo '901nosession';
	}

	//CHECK LESSON IS BOOKMARK
	public function getBookmark() {
		if(!$this->session->userdata('username')) {
			return false;
		}
		$username = $this->session->userdata('username');
		$this->load->model('Model_user');	
		$cursor = $this->Model_user->getBookmark($username);
		$arr = [];
		foreach($cursor['bookmark'] as $ls ) {
			array_push($arr, $ls);
		}
		echo json_encode($arr);
	}

	public function addBookmark($lsId) {
		if(!$this->session->userdata('username')) {
			return false;
		}
		$username = $this->session->userdata('username');
		$this->load->model('Model_user');	
		$cursor = $this->Model_user->getBookmark($username);
		$arr = [];
		foreach($cursor['bookmark'] as $ls ) {
			array_push($arr, $ls);
		}
		array_push($arr, $lsId);
		$ate = $this->Model_user->addBookmark($username, $arr);
		echo 'success';
	}

	public function removeBookmark($lsId) {
		if(!$this->session->userdata('username')) {
			return false;
		}
		$username = $this->session->userdata('username');
		$this->load->model('Model_user');	
		$cursor = $this->Model_user->getBookmark($username);
		$arr = [];
		foreach($cursor['bookmark'] as $ls ) {
			array_push($arr, $ls);
		}
		
		if (($key = array_search($lsId, $arr)) !== false) {
			unset($arr[$key]);
		}

		$ate = $this->Model_user->addBookmark($username, $arr);
		echo 'success';
	}

	//DESTROY SESSION
	public function destroy_session(){
		session_id('uid');
		session_start();
		session_destroy();
		session_commit();
	}

}

?>